<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <%@include file="/common/common.jsp" %>

    <style type="text/css">
        .close-pdf{position:fixed; top:20px; right:20px; color:#1d69b3; }
        button{width:40px; border:none; background: none; color:#1d69b3; font-size: 20px; outline: none;}
        .container{position: relative;}
        .control{
            position: fixed;
            bottom: 80px;
            left: 50%;
            margin-left: -140px;
        }
        .control span{color:#1d69b3; font-size: 16px; line-height: 30px;}
    </style>
</head>

<body>
<div class="container">
<div class="row">
    <a href="javascript:;" onclick="history.go(-1)" class="close-pdf"> <i class="glyphicon glyphicon-remove"></i></a>
    <canvas id="the-canvas" style="width: 100%;height: 95%;"></canvas>
</div>
<div class="row control" style="text-align: center;">
    <button id="first"><i class="fa fa-angle-double-left"></i></button>
    &nbsp; &nbsp;
    <button id="prev"><i class="fa fa-angle-left"></i></button>
    &nbsp; &nbsp;
    <span><span id="page_num"></span> / <span id="page_count"></span></span>
    &nbsp; &nbsp;
    <button id="next"><i class="fa fa-angle-right"></i></button>
    &nbsp; &nbsp;
    <button id="final"><i class="fa fa-angle-double-right"></i></button>
</div>
</div>
<%--<div class="control">
    <button id="first"><i class="fa fa-angle-double-left"></i></button>
    &nbsp; &nbsp;
    <button id="prev"><i class="fa fa-angle-left"></i></button>
    &nbsp; &nbsp;
    <span><span id="page_num"></span> / <span id="page_count"></span></span>
    &nbsp; &nbsp;
    <button id="next"><i class="fa fa-angle-right"></i></button>
    &nbsp; &nbsp;
    <button id="final"><i class="fa fa-angle-double-right"></i></button>
</div>--%>

<script src="${basePath}/js/common/pdf.js"></script>
<script src="${basePath}/js/common/pdf.worker.js"></script>
<script>
    //var url = '/pdf/doc/demo1.pdf';
    var url = '${url}';

    var pdfDoc = null,
            pageNum = 1,
            pageRendering = false,
            pageNumPending = null,
            scale = 2.0,
            canvas = document.getElementById('the-canvas'),
            ctx = canvas.getContext('2d');


    /**
     * Get page info from document, resize canvas accordingly, and render page.
     * @param num Page number.
     */
    function renderPage(num) {
        pageRendering = true;
        // Using promise to fetch the page
        pdfDoc.getPage(num).then(function(page) {
            var viewport = page.getViewport(scale);
            canvas.height = viewport.height;
            canvas.width = viewport.width;

            // Render PDF page into canvas context
            var renderContext = {
                canvasContext: ctx,
                viewport: viewport
            };
            var renderTask = page.render(renderContext);

            // Wait for rendering to finish
            renderTask.promise.then(function() {
                pageRendering = false;
                if (pageNumPending !== null) {
                    // New page rendering is pending
                    renderPage(pageNumPending);
                    pageNumPending = null;
                }
            });
        });

        // Update page counters
        document.getElementById('page_num').textContent = num;
    }

    /**
     * If another page rendering in progress, waits until the rendering is
     * finised. Otherwise, executes rendering immediately.
     */
    function queueRenderPage(num) {
        if (pageRendering) {
            pageNumPending = num;
        } else {
            renderPage(num);
        }
    }

    /**
     * Displays previous page.
     */
    function onPrevPage() {
        if (pageNum <= 1) {
            return;
        }
        pageNum--;
        queueRenderPage(pageNum);
    }
    document.getElementById('prev').addEventListener('click', onPrevPage);

    /**
     * Displays next page.
     */
    function onNextPage() {
        if (pageNum >= pdfDoc.numPages) {
            return;
        }
        pageNum++;
        queueRenderPage(pageNum);
    }
    document.getElementById('next').addEventListener('click', onNextPage);

    /**
     * Displays first page.
     */
    function onFirstPage() {
        if (pageNum <= 1) {
            return;
        }
        pageNum=1;
        queueRenderPage(pageNum);
    }
    document.getElementById('first').addEventListener('click', onFirstPage);

    /**
     * Displays final page.
     */
    function onFinalPage() {
        if (pageNum >= pdfDoc.numPages) {
            return;
        }
        pageNum=pdfDoc.numPages;
        queueRenderPage(pageNum);
    }
    document.getElementById('final').addEventListener('click', onFinalPage);

    /**
     * Asynchronously downloads PDF.
     */
    PDFJS.getDocument(url).then(function(pdfDoc_) {
        pdfDoc = pdfDoc_;
        document.getElementById('page_count').textContent = pdfDoc.numPages;

        // Initial/first page rendering
        renderPage(pageNum);
    });
</script>
</body>
</html>